/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.common.utils.enums.Status;
import com.foreveross.bsl.system.application.AccountActivatingCheckCodeApplication;
import com.foreveross.bsl.system.application.vo.AccountActivatingCheckCodeVO;
import com.foreveross.bsl.system.model.domain.AccountActivatingCheckCode;


/**
 * @description
 *
 * @author rening
 * @version v1.0
 *
 * @2013-6-7
 *
 */
@Named("accountActivatingCheckCodeApplication")
public class AccountActivatingCheckCodeImpl implements AccountActivatingCheckCodeApplication {

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public AccountActivatingCheckCodeVO saveEntity(AccountActivatingCheckCodeVO entity) {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		BeanHelper.copyProperties(accountActivatingCheckCode, entity);
		accountActivatingCheckCode.save();
		entity.setId(accountActivatingCheckCode.getId());
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public AccountActivatingCheckCodeVO updateEntity(String id, AccountActivatingCheckCodeVO entity) {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		accountActivatingCheckCode = (AccountActivatingCheckCode)accountActivatingCheckCode.findOne(id);
		BeanHelper.copyProperties(accountActivatingCheckCode, entity);
		accountActivatingCheckCode.save();
		BeanHelper.copyProperties(entity, accountActivatingCheckCode);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(String id) {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		accountActivatingCheckCode = (AccountActivatingCheckCode)accountActivatingCheckCode.findOne(id);
		accountActivatingCheckCode.delete();
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public AccountActivatingCheckCodeVO getEntity(String id) {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		accountActivatingCheckCode = (AccountActivatingCheckCode)accountActivatingCheckCode.findOne(id);
		AccountActivatingCheckCodeVO vo = new AccountActivatingCheckCodeVO();
		BeanHelper.copyProperties(vo, accountActivatingCheckCode);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public List<AccountActivatingCheckCodeVO> getEntityByCheckCode(String checkcode) {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		OperatEnum operat1 = OperatEnum.EQ;
		OperatEnum operat2 = OperatEnum.EQ;
		OperatEnum[] operats = new OperatEnum[]{operat1,operat2};
		String proName1 = "checkCode";
		String proName2 = "status";
		String[] proNames = new String[]{proName1,proName2};
		String[] values = new String[]{checkcode,Status.YES.getValue()};
		List<AccountActivatingCheckCode> accountActivatingCheckCodes = (List<AccountActivatingCheckCode>)accountActivatingCheckCode.findListByProperty(proNames, operats,values);
		if(accountActivatingCheckCodes != null){
			List<AccountActivatingCheckCodeVO> result = new ArrayList<AccountActivatingCheckCodeVO>();
			AccountActivatingCheckCodeVO vo;
			for (Iterator iterator = accountActivatingCheckCodes.iterator(); iterator.hasNext();) {
				AccountActivatingCheckCode obj = (AccountActivatingCheckCode) iterator.next();

				vo = new AccountActivatingCheckCodeVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}


	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<AccountActivatingCheckCodeVO> findAll() {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		Iterable iterable = accountActivatingCheckCode.findAll();
		if(iterable != null){
			List<AccountActivatingCheckCodeVO> result = new ArrayList<AccountActivatingCheckCodeVO>();
			AccountActivatingCheckCodeVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				AccountActivatingCheckCode obj = (AccountActivatingCheckCode) iterator.next();

				vo = new AccountActivatingCheckCodeVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<AccountActivatingCheckCodeVO> findByPageable(Pageable pageable) {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		Page page = accountActivatingCheckCode.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<AccountActivatingCheckCodeVO> result = new ArrayList<AccountActivatingCheckCodeVO>();
			AccountActivatingCheckCodeVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				AccountActivatingCheckCode obj = (AccountActivatingCheckCode) iterator.next();

				vo = new AccountActivatingCheckCodeVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<AccountActivatingCheckCodeVO> pageVO = new PageImpl<AccountActivatingCheckCodeVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public List<AccountActivatingCheckCodeVO> findRecordsByAccountId(String accountId) {
		AccountActivatingCheckCode accountActivatingCheckCode = new AccountActivatingCheckCode();
		List<AccountActivatingCheckCode> checkCodeList = accountActivatingCheckCode.findListByProperty("accountId", OperatEnum.EQ, accountId);
		if(checkCodeList != null && !checkCodeList.isEmpty()){
			List<AccountActivatingCheckCodeVO> result = new ArrayList<AccountActivatingCheckCodeVO>();
			AccountActivatingCheckCodeVO vo;
			for (Iterator iterator = checkCodeList.iterator(); iterator.hasNext();) {
				AccountActivatingCheckCode obj = (AccountActivatingCheckCode) iterator.next();
	
				vo = new AccountActivatingCheckCodeVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

}
